<template>
	<view class="goods_list">
		<good-list @goodsItemClick="goGoodsDetail" :goods="goods"></good-list>
		<view class="isOver" v-if="flag">
			------到底了------
		</view>
	</view>
</template>

<script>
	import goodList from '../../components/goods-list/goods-list.vue'
	export default {
		data() {
			return {
				pageindex:1,
				goods:[],
				flag:false
			}
		},
		methods: {
			// 獲取商品列表數據
			async getGoodsList(callBack){
				const res=await this.$myRequest({
						url:'api/getgoods?pageindex='+this.pageindex
					})
					this.goods=[...this.goods,...res.data.message];
					callBack && callBack()
				},
				
				goGoodsDetail(id){
					uni.navigateTo({
						url:'/pages/goods_detail/goods_detail?id='+id
					})
				}
			},
			
		components:{
			"good-list":goodList
		},
		
		onLoad() {
			this.getGoodsList()
		},
		
		onReachBottom() {
			if(this.goods.length<this.pageindex*10){
				this.flag=true
				return
			}
			this.pageindex++
			this.getGoodsList()
		},
		
		onPullDownRefresh() {
			this.pageindex=1
			this.goods=[]
			this.flag=false
			setTimeout(()=>{
				this.getGoodsList(()=>{
					uni.stopPullDownRefresh()
				})
			},1000)
		}
	}
</script>

<style lang="scss">
	.goods_list{
		background: #eee;
	}
	.isOver{
		width: 100%;
		height: 50px;
		line-height: 50px;
		text-align: center;
		font-size: 28rpx;
	}
</style>
